package br.com.firula.threads;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BreakSystem extends Thread{
	private final long sleepInterval = 5000; // 5 segundos, antes 3 minutos
	private final double limiteFila = 40.0;
	private final int mediaConsiderada = 0; //[0|1|2] 0.00, 0.00, 0.00; Obtemos o load mais atualizado
	
	private static boolean desativarFuncoesCriticas = false;
	public static boolean desativarFuncoesCriticas(){
		return desativarFuncoesCriticas;
	}
	
	public void run() {
		if(!System.getProperty("os.name").contains("Windows")) {
			while(true) {
				try {
					sleep(sleepInterval);
					BufferedReader bf = new BufferedReader((new FileReader(File.separator+"proc"+File.separator+"loadavg")));
					String line = bf.readLine();
					
					double loadavg = Double.parseDouble(line.split(" ")[mediaConsiderada]);
					desativarFuncoesCriticas = loadavg >= limiteFila;
					
					System.out.println("Break: "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" -avg: "+loadavg+" -ativo: "+desativarFuncoesCriticas);
					
					bf.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
